package lium.lbs.lbsim.activity

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.widget.Toast
import com.buz.hjcdriver.bean.GetUserInfoResultBean
import com.lmlibrary.Constants
import lium.lbs.lbsim.utils.UserUtils
import lium.lbs.lbsim.utils.UserUtils.setUserData
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.JsonCallback
import com.lzy.okgo.model.Response
import lium.lbs.lbsim.jgchat.utils.photovideo.takevideo.utils.LogUtils
import lium.lbs.lbsim.utils.SpUtils
import java.util.HashMap

class MainActivity : MainBaseActivity() {
    private var isLogin: Boolean = false
    private var exitTime: Long = 0

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        isLogin = SpUtils.getInstance().get(Constants.isLogin, false) as Boolean
        if (!isLogin) {
            startActivity(Intent(this@MainActivity, LoginActivity::class.java))
        }
        getmyinfo()
    }

    override fun onClick(v: View?) {
        super.onClick(v)
    }

    private fun getmyinfo() {
        //隐示获取个人信息
        val param = HashMap<String, String>()
        postData("/member/getMemberInfo", param, object : JsonCallback<ResponseBean<GetUserInfoResultBean>>(this) {
            override fun onSuccess(response: Response<ResponseBean<GetUserInfoResultBean>>) {
                UserUtils.login(response.body().data.memberId + "")


                setUserData(
                    response.body().data.memberId,
                    response.body().data.nickName,
                    response.body().data.headImage,
                    response.body().data.phone
                )
            }
        })
    }
    // 再按一次退出程序
     override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(applicationContext, "再按一次退出程序", Toast.LENGTH_SHORT).show()
                exitTime = System.currentTimeMillis()
            } else {
                finish()
                System.exit(0)
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}